<?php
/**
 * “腾讯微博” 刷粉丝
 * 
 * @author linsir123
 */
class LxsTWeiBo
{
	/**
	 * @var 收听的粉丝列表
	 */
	public $userList;
	
	/**
	 * @var 收听列表
	 */
	public $idolList = array();
	
	/**
	 * @var 收听列表(缓存文件)
	 */
	public $idolFile;
	
	/**
	 * 构造函数
	 */
	public function __construct($apps, $userList = array(), $index = 0)
	{
		ob_start();
		session_start();
		set_time_limit(0);
		set_include_path(dirname(dirname(__FILE__)) .'/lib/');
		header('Content-Type: text/html; charset=utf-8');
		if (function_exists('date_default_timezone_set')) 
			date_default_timezone_set('Asia/ShangHai');
			
		///
		include(dirname(__FILE__) .'/app/'. $apps[0][0]);
		include('OpenSDK/Tencent/Weibo.php');
		
		///
		$_SESSION = $apps[0][1];
		OpenSDK_Tencent_Weibo::init($appkey, $appsecret);
		pr("现在时间: ". date('Y-m-d H:i:s'));
		pr("使用的Key: ". $appkey);
		
		///
		$this->idolFile = sprintf(dirname(__FILE__) .'/_idollist_%d.php', $index);
		$this->idolList = $this->_getIdolList();
		$this->userList = $userList;
	}
	
	/**
	 * 开始
	 */
	function run()
	{
		$idolNum = count($this->idolList);
		
		///
		pr("关注数: ". $idolNum);
		if ($idolNum > 800) {
			$this->unFollow(10);
			$this->follow(10);
		} else 
			$this->follow(15);
			
		///
		$this->_saveCache();
	}
	
	/**
	 * 收听某个用户的粉丝
	 */
	function follow($reqnum = 10)
	{
		$startIndex = mt_rand(0, 300);
		$userName = $this->_getFollowUser();
		pr("----------------------follow[{$userName}|{$startIndex}]----------------------");
		
		///
		$data = OpenSDK_Tencent_Weibo::call('friends/user_fanslist', 
			array('startindex'=>$startIndex,'name'=>$userName,'reqnum'=>$reqnum));
			
		foreach ($data['data']['info'] as $v) {
			$name = trim($v['name']);
			
			///
			if ($this->_checkIdoNum(true)) 
				break;
				
			if (empty($name) || in_array($name, $this->idolList)) 
				continue;
				
			///
			$result = OpenSDK_Tencent_Weibo::call('friends/add', array('name'=>$name), 'POST');
			$this->_log(json_encode($result)."    ".$name, '_follow');
			$code = $this->_getCode($result);
			if ($code === 0) 
				array_push($this->idolList, $name);
		}
	}
	
	/**
	 * 取消收听
	 */
	function unFollow($reqnum = 10)
	{
		pr("----------------------unFollow----------------------");
		
		///
		$num = 0;
		foreach ($this->idolList as $i => $name) {
			$name = trim($name);
			
			///
			if (++$num > $reqnum || $this->_checkIdoNum(false)) 
				break;
				
			///
			$result = OpenSDK_Tencent_Weibo::call('friends/del', array('name'=>$name), 'POST');
			$this->_log(json_encode($result)."    ".$name, '_unFollow');
			$code = $this->_getCode($result);
			if ($code !== false) {
				if ($code === 0 || $code === 80022) 
					unset($this->idolList[$i]);
			}
		}
	}
	
	/**
	 * 检测
	 */
	private function _checkIdoNum($plus = true, $max = 800)
	{
		$count = count($this->idolList);
		if ($plus) 
			return $count >= $max;
			
		return  $count <= $max;
	}
	
	/**
	 * 验证结果
	 */
	private function _getCode($result)
	{
		if (isset($result['errcode'])) 
			return intval($result['errcode']);
			
		///
		return false;
	}
	
	/**
	 * 取得收听列表
	 */
	private function _getIdolList()
	{
		if ( ! file_exists($this->idolFile)) {
			$i = 0;
			$list = array();
			while ($i < 2000) {
				$data = OpenSDK_Tencent_Weibo::call('friends/idollist_name', array('reqnum'=>200, 'startindex'=>$i));
				if ($data['data']['hasnext'] == 1) 
					break;
					
				///
				$i += 200;
				if (is_array($data['data']['info'])) {
					foreach ($data['data']['info'] as $v) {
						if ( ! empty($v['name']) && ! in_array($v['name'], $list)) 
							$list[] = $v['name'];
					}
				}
			}
			
			///
			$this->_saveCache($list);
		} else 
			$list = require($this->idolFile);
			
		///
		return $list;
	}
	
	/**
	 * 取得某个名人
	 */
	private function _getFollowUser()
	{
		$list =	array (
			/* 星座 */
			'xingzuoxwz',
			'mudan',
			
			/* 笑话 */
			'lengxiaohua',
			'leng-xiao-hua',
			
			/* 娱乐 */
			'hejiong',
			'karenmo',
			'caikangyong',
			'loveshuqiforever',
			'xie_na',
			'sunyanzi',
			'xujinglei',
			'michelejiaxin',
			'mayili007',
			'carinalau1208',
			'wuyanzu',
			'aaronkwok999',
			'yangmiblog0912',
			'mengfei',
			'duhaitao',
			'linxinru',
			'wuxinxin',
		);
		if ( !empty($this->userList)) 
			$list = $this->userList;
			
		///
		$i = mt_rand(0, count($list)-1);
		return $list[$i];
	}
	
	/**
	 * 日志
	 */
	private function _log($content, $name = '_follow')
	{
		static $i = 0;
		
		///
		pr($i++ .'   '. $content);
		sleep(2);
	}
	
	/**
	 * 保存缓存
	 */
	private function _saveCache($list = null)
	{
		if (empty($list)) 
			$list = $this->idolList;
			
		///
		$content = "<?php \r\n return ".var_export(array_values($list), true).";";
		file_put_contents($this->idolFile, $content);
	}
}




/**
 * 调试
 */
function pr($var)
{
	echo "<pre>";
	print_r($var);
	echo "</pre>\r\n";
	ob_flush();
    flush();
}
